<?php

class Application_View_Helper_Breakword {
		
	public function breakword($txt,$len,$delim='\s;,.!?:#') {
		$txt = preg_replace_callback ("#(</?[a-z]+(?:>|\s[^>]*>)|[^<]+)#mi"
		,create_function('$a'
		,'static $len = '.$len.';'
		.'$len1 = $len-1;'
		.'$delim = \''.str_replace("#","\\#",$delim).'\';'
		.'if ("<" == $a[0]{0}) return $a[0];'
		.'if ($len<=0) return "";'
		.'$res = preg_split("#(.{0,$len1}+(?=[$delim]))|(.{0,$len}[^$delim]*)#ms",$a[0],2,PREG_SPLIT_DELIM_CAPTURE);'
		.'if ($res[1]) { $len -= strlen($res[1])+1; $res = $res[1];}'
		.'else         { $len -= strlen($res[2]); $res = $res[2];}'
		.'$res = rtrim($res);/*preg_replace("#[$delim]+$#m","",$res);*/'
		.'return $res;')
		,$txt);
		while (preg_match("#<([a-z]+)[^>]*>\s*</\\1>#mi",$txt)) {
		$txt = preg_replace("#<([a-z]+)[^>]*>\s*</\\1>#mi","",$txt);
		}
		return $txt;
	}
}